/*
Copyright (c) (2012) (Anthony Bigot, Vincent Loppin)
Permission is hereby granted, free of charge, to any person obtaining a copy of this software
and associated documentation files (the "Software"), to deal in the Software without restriction,
including without limitation the rights to use, copy, modify, merge, publish, distribute,
sublicense, and/or sell copies of the Software, and to permit persons to whom the Software
is furnished to do so, subject to the following conditions: The above copyright notice and
this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

#ifndef VEC3_H
#define VEC3_H

#include <iostream>

namespace Util{

/*! \class Vec3<T>
  * \brief Manage vectors 3D
  * \author Anthony BIGOT & Vincent LOPPIN
  * \version 1.0
  *
  */
    template <class T>
    class Vec3 {
    public:
        Vec3()
            {
                v[0] = 0;		v[1] = 0;		v[2] = 0;
            }

            Vec3(T _x, T _y, T _z)
            {
                v[0] = _x;		v[1] = _y;		v[2] = _z;
            }

            /// Vector add
            inline const Vec3<T> operator + (const Vec3<T> & vec) const{
                return Vec3<T>(	vec.v[0] + v[0],
                                vec.v[1] + v[1],
                                vec.v[2] + v[2]);
            }

            /// Vector substract
            inline const Vec3<T> operator - (const Vec3<T> & vec) const{
                return Vec3<T>(	v[0] - vec.v[0],
                                v[1] - vec.v[1],
                                v[2] - vec.v[2]);
            }

            /// Product with a scalar
            inline const Vec3<T> operator * (const T sca) const{
                return Vec3<T>(v[0]*sca,v[1]*sca,v[2]*sca);
            }

            /// Divide by a scalar
            inline const Vec3<T> operator / (const T sca) const{
                return Vec3<T>(v[0]/sca,v[1]/sca,v[2]/sca);
            }

            /// Dot product
            inline const T operator * (const Vec3<T> & vec) const{
                return v[0]*vec.v[0] + v[1]*vec.v[1] + v[2]*vec.v[2];
            }

            /// Cross product
            inline const Vec3<T> operator ^ (const Vec3<T> & vec) const{
                return Vec3<T>(	v[1]*vec.v[2] - v[2]*vec.v[1],
                                v[2]*vec.v[0] - v[0]*vec.v[2],
                                v[0]*vec.v[1] - v[1]*vec.v[0]	);
            }

            inline const Vec3<T> operator - () const
            {
                return Vec3<T>(-v[0],-v[1],-v[2]);
            }

            inline T&	x()			{ return v[0]; }
            inline T	x() const	{ return v[0]; }
            inline T&	y()			{ return v[1]; }
            inline T	y() const	{ return v[1]; }
            inline T&	z()			{ return v[2]; }
            inline T	z() const	{ return v[2]; }

            inline void reset(){ v[0] = 0; v[1] = 0; v[2] = 0; }

            inline void print(std::ostream &o) const { o << "[" << v[0] << ";" << v[1] << ";" << v[2] << "]"; }

            inline Vec3<T> & operator= (const Vec3<T> & vec)
            {
                if(this == &vec) return *this;
                v[0] = vec.v[0];		v[1] = vec.v[1];		v[2] = vec.v[2];
                return *this;
            }

            /// return square lenght
            inline T length2() const { return v[0]*v[0] + v[1]*v[1] + v[2]*v[2]; }

            /// return lenght
            inline T length() const { return sqrt(length2()); }

            /// normalize vector
            inline void normalize(){
                T len(length());
                v[0] /= len;		v[1] /= len;		v[2] /= len;
            }

            /// linear interpolation
            inline Vec3<T> linearInterpolation(const Vec3<T> &dst, float u) const
            {
                return Vec3<T>(	v[0]*(1.f-u) + dst.v[0]*u,
                                v[1]*(1.f-u) + dst.v[1]*u,
                                v[2]*(1.f-u) + dst.v[2]*u);
            }

            inline bool isNull(){
                return (	abs(v[0]) < 0.001 &&
                            abs(v[1]) < 0.001 &&
                            abs(v[2]) < 0.001);
            }

            inline T * ptr(){ return (T*)v; }
            inline T * ptr() const{ return (T *)v; }
        protected:
            T v[3];
    };

    template<typename T>
    std::ostream & operator<< (std::ostream & o, const Vec3<T> & v)
    {
        o.precision(5);
        o << std::fixed;
        o << "[" << v.ptr()[0] << ";" << v.ptr()[1] << ";" << v.ptr()[2] << "]" << std::endl;
        return o;
    }

}

typedef Util::Vec3<double>        Vec3d;
typedef Util::Vec3<int>           Vec3i;
typedef Util::Vec3<unsigned int>  Vec3ui;
typedef Util::Vec3<float>         Vec3f;
typedef Util::Vec3<unsigned char> Vec3uc;


#endif // VEC3_H
